package com.example.cardgame

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment

const val name="name"
const val sex="sex"
const val num="num"
const val phone="phone"
const val email="email"
const val hobby="hobby"
var score1:Int=0
var score2:Int=0
class frameLayout : Fragment() {
    private lateinit var startForResult: ActivityResultLauncher<Intent>
    override fun onCreateView(
        inflater: LayoutInflater,container:ViewGroup?,savedInstanceState: Bundle?
    ): View?{
//        val information:Information?=arguments?.getSerializable("information") as? Information
        return inflater.inflate(R.layout.activity_main,container,false)
    }
    companion object{
        @JvmStatic
        fun newInstance():frameLayout=frameLayout()
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        if(arguments!=null){
            score1 =arguments?.getInt("score") as Int
            score2 =arguments?.getInt("score2") as Int
        }
        super.onViewCreated(view, savedInstanceState)
        val score1_text=view.findViewById<TextView>(R.id.score12_text)
        val score2_text=view.findViewById<TextView>(R.id.score22_text)
        val nameText = view.findViewById<TextView>(R.id.name2_text)
        val numText = view.findViewById<TextView>(R.id.number_text2_text)
        val phoneText = view.findViewById<TextView>(R.id.phone2_text)
        val emailText = view.findViewById<TextView>(R.id.email2_text)
        val sexText = view.findViewById<TextView>(R.id.sex2_text)
        val hobbyText = view.findViewById<TextView>(R.id.hobby2_text)
        val imageView = view.findViewById<ImageView>(R.id.imageView2)
        val a:Int=Integer.valueOf(score1_text.text.toString())
        if(a<score1)
        {
            score1_text.text= score1.toString()
        }
        val c:Int=Integer.valueOf(score2_text.text.toString())
        if(c<score2){
            score2_text.text= score2.toString()
        }
        startForResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            if (result.resultCode == 1) {
                // 处理从Activity返回的数据
                    // 显示返回的数据，或者进行其他处理
                Log.i("frameLayout","$result.data")
                nameText.text = result.data?.getStringExtra(name)
                numText.text = result.data?.getStringExtra(num)
                phoneText.text = result.data?.getStringExtra(phone)
                emailText.text = result.data?.getStringExtra(email)
                sexText.text = result.data?.getStringExtra(sex)
                hobbyText.text = result.data?.getStringExtra(hobby)
                val bitmap = result.data?.getParcelableExtra<Bitmap>("bitmap")
                imageView.setImageBitmap(bitmap)

            }
        }
        val button_edit=view.findViewById<Button>(R.id.edit_button)
        button_edit.setOnClickListener{
            val intent=Intent(requireContext(),InformationActivity::class.java)
            startForResult.launch(intent)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==0) {
            if (resultCode == 1) {


            }
        }
    }

//        }else{
//            if(resultCode==2)
//            {
//                val scoreText=view?.findViewById<TextView>(R.id.score2_text)
//                val a: Int = Integer.valueOf(scoreText?.text.toString())
//                val b:Int =Integer.valueOf(data?.getStringExtra(score))
//                if(a<b)
//                {
//                    scoreText?.text=data?.getStringExtra(score)
//                }
//            }
//        }
//    }
}